<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
function trim(s){
    // "    aflasj  lj      "
    // 查找左下标
    var fromIndex = 0;
    for (var i = 0; i < s.length; i++){
        fromIndex = i;
        var c = s[i];
        if (c != " " && c != "\t" && c != "\n" && c != "\r"){
            break;
        }
    }
    // 查找右下标
    var toIndex = 0;
    for (var i = s.length - 1; i >= 0; i--){
        toIndex = i;
        var c = s[i];
        if (c != " " && c != "\t" && c != "\n" && c != "\r"){
            break;
        }
    }
    // 截取字符串
    return s.slice(fromIndex, toIndex + 1);
}

console.log("z" + trim(" \t  \n \r     \n \r \t ") + "z");
</script>
</body>
</html>
<!--
    作者：李振超  2017年7月27日08:24
    去除字符串首尾空格.(写一个自定义的 trim 函数)
-->